// pages/my/integral/index.js
var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
  data: {
    orderList: [],
    goodlist:[],
    order_tab: [
      '积分商城',
      '积分订单'
    ],
    select: 0,
    numList: [],
    isEmpty: false,
    img_url: c.img_url,
  },

  onLoad: function (options) {
    if (options.select) {
      this.setData({
          select: 1
        });
    }
    utils.checkLogin();
    this.getGoodspoint();
    this.getOrder();
  },
  onShow: function () {
    //this.getGoodspoint();
  },
   //下拉刷新
  onPullDownRefresh: function () {
    if (this.data.select==0) {
      this.getGoodspoint();
    }else{
      this.getOrder(); 
    }
  },
  //积分商城
  getGoodspoint: function () {
    var that = this;
    var data = {
      url: "/v1/goodspoint/lists",
      token: wx.getStorageSync("userinfo").wap_token,
      page: 1,
      limit: 100,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        var isEmpty = JSON.stringify(data) == '{}' ? true : false;
        that.setData({
          goodlist: data,
          isEmpty: isEmpty,
        });
      }
      wx.stopPullDownRefresh();
    });
  },
  //积分订单
  getOrder: function () {
    var that = this;
    var data = {
      url: "/v1/ordergoodspoint/lists",
      token: wx.getStorageSync("userinfo").wap_token,
      page: 1,
      limit: 100,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        var isEmpty = JSON.stringify(data) == '{}' ? true : false;
        that.setData({
          orderList: data,
          isEmpty: isEmpty
        });
      }
      wx.stopPullDownRefresh();
    });
  },
  Confirmed: function (e) {
    var order_no = e.currentTarget.dataset.order_no;
    var that = this;
    wx.showModal({
      title: '提示',
      cancelText: '取消',
      confirmText: '确定',
      content: '是否确认收货？',
      success: function (res) {
        if (res.confirm) {
          var data = {
            url: "/v1/ordergoodspoint/finish",
            token: wx.getStorageSync("userinfo").wap_token,
            order_no: order_no,
          };
          utils.request(data).then(function (res) {
            if (res.code == 0) {
              utils.sucMsg(res.msg);
              setTimeout(function () {
                that.getOrder();
              }, 1500);
            }else{
              utils.errMsg(res.msg);
            }
          });
        }
      }
    })
  },
  
  selectTab: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      select: index,
    })
    if (index==0) {
      this.getGoodspoint();
    }else{
      this.getOrder(); 
    }
  }
})